package com.hzlj.position.locate.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fz.common.base.converter.QueryWrapperConverterPlus;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.google.common.collect.Lists;
import com.hzlj.position.locate.common.dto.amtStatJzryReportAudio.AmtStatJzryReportAudioHandleDTO;
import com.hzlj.position.locate.common.dto.amtStatJzryReportAudio.AmtStatJzryReportAudioQueryParamDTO;
import com.hzlj.position.locate.common.dto.amtStatJzryReportAudio.AmtStatJzryReportAudioQueryResultDTO;
import com.hzlj.position.locate.common.model.AmtStatJzryReportAudio;
import com.hzlj.position.locate.mapper.AmtStatJzryReportAudioMapper;
import com.hzlj.sqjz.config.common.enums.HandleStatus;
import com.hzlj.sqjz.jz.common.constant.JzJoin;
import com.hzlj.sqjz.jz.common.model.JzJbxx;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;

/**
 * 语音汇报-按人按月统计(AmtStatJzryReportAudio)表操作类
 *
 * @author makejava
 * @date 2024-09-06 10:49:07
 */
@Repository
public class AmtStatJzryReportAudioDao extends ServiceImpl<AmtStatJzryReportAudioMapper, AmtStatJzryReportAudio> {
    @Resource
    private AmtStatJzryReportAudioMapper amtStatJzryReportAudioMapper;


    /**
     * 查询列表：不分页
     */
    public List<AmtStatJzryReportAudioQueryResultDTO> listAmtStatJzryReportAudio(AmtStatJzryReportAudioQueryParamDTO dto) {
        return queryWrapper()
                .list(AmtStatJzryReportAudioQueryResultDTO.class, dto, queryWrapperWhere(dto));
    }

    /**
     * 查询列表：分页
     */
    public QueryPageResultDTO<AmtStatJzryReportAudioQueryResultDTO> pageAmtStatJzryReportAudio(AmtStatJzryReportAudioQueryParamDTO dto, PageParamDTO page) {
        return queryWrapper()
                .page(AmtStatJzryReportAudioQueryResultDTO.class, dto, page, queryWrapperWhere(dto));
    }

    private QueryWrapperConverterPlus<AmtStatJzryReportAudio> queryWrapper() {
        return QueryWrapperConverterPlus.of(amtStatJzryReportAudioMapper)
                .innerJoin(JzJbxx.class, JzJoin.jz(AmtStatJzryReportAudio::getJzId));
    }

    private Consumer<MPJLambdaWrapper<AmtStatJzryReportAudio>> queryWrapperWhere(AmtStatJzryReportAudioQueryParamDTO dto) {
        return q -> q
                .eq(Boolean.TRUE.equals(dto.getReported()),
                        AmtStatJzryReportAudio::getHandleStatus,
                        HandleStatus.NONE.getStatus())
                .eq(Boolean.TRUE.equals(dto.getReportedPhone()),
                        AmtStatJzryReportAudio::getHandleStatusPhone,
                        HandleStatus.NONE.getStatus())
                .in(Boolean.FALSE.equals(dto.getReported()),
                        AmtStatJzryReportAudio::getHandleStatus,
                        Lists.newArrayList(HandleStatus.WAIT.getStatus(), HandleStatus.DONE.getStatus()))
                .in(Boolean.FALSE.equals(dto.getReportedPhone()),
                        AmtStatJzryReportAudio::getHandleStatusPhone,
                        Lists.newArrayList(HandleStatus.WAIT.getStatus(), HandleStatus.DONE.getStatus()));
    }


    public void handle(AmtStatJzryReportAudioHandleDTO dto, AmtStatJzryReportAudioHandleDTO.Key key) {
        LambdaUpdateWrapper<AmtStatJzryReportAudio> updateWrapper = new UpdateWrapper<AmtStatJzryReportAudio>().lambda()
                .set(AmtStatJzryReportAudio::getHandleStatus, dto.getHandleStatus())
                .set(AmtStatJzryReportAudio::getHandleTime, dto.getHandleTime())
                .set(ObjectUtil.isNotEmpty(dto.getHandleExt()), AmtStatJzryReportAudio::getHandleExt, JsonUtils.obj2json(dto.getHandleExt()))
                .eq(AmtStatJzryReportAudio::getBelongMonth, key.getBelongMonth())
                .eq(AmtStatJzryReportAudio::getJzId, key.getJzId())
                .eq(AmtStatJzryReportAudio::getBelongWeek, key.getBelongWeek());
        this.update(updateWrapper);
    }

    public void handlePhone(AmtStatJzryReportAudioHandleDTO dto, AmtStatJzryReportAudioHandleDTO.Key key) {
        LambdaUpdateWrapper<AmtStatJzryReportAudio> updateWrapper = new UpdateWrapper<AmtStatJzryReportAudio>().lambda()
                .set(AmtStatJzryReportAudio::getHandleStatusPhone, dto.getHandleStatus())
                .set(AmtStatJzryReportAudio::getHandleTimePhone, dto.getHandleTime())
                .set(ObjectUtil.isNotEmpty(dto.getHandleExt()), AmtStatJzryReportAudio::getHandleExtPhone, JsonUtils.obj2json(dto.getHandleExt()))
                .eq(AmtStatJzryReportAudio::getBelongMonth, key.getBelongMonth())
                .eq(AmtStatJzryReportAudio::getJzId, key.getJzId())
                .eq(AmtStatJzryReportAudio::getBelongWeek, key.getBelongWeek());
        this.update(updateWrapper);
    }
}

